package com.bigchun.woodhunter;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import bean.ContentDataBean;
import myapp.MyApp;
import util.PreferenceUtil;
import util.SystemStatusManager;

public class AboutUsActivity extends BaseActivity {


    private LinearLayout ll_reback;
    private MyApp myapp;
    private Context context;
    private String token;
    private String z_about;
    private String e_about;
    private String domain;
    private WebView webview;
    private String yuyan = "";
    private String apptype = "";
    private String textData;
    private TextView tv_title;
    private String j_about;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_aboutus);
        context = this;
        myapp = (MyApp) getApplicationContext();
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        textData = sharedPreferences.getString("textData", "");
        token = sharedPreferences.getString("token", "");
        apptype = sharedPreferences.getString("apptype", "0");
        z_about = sharedPreferences.getString("z_about", "");
        e_about = sharedPreferences.getString("e_about", "");
        j_about = sharedPreferences.getString("j_about", "");
        domain = sharedPreferences.getString("domain", "");
        yuyan = PreferenceUtil.getString("language", "zh");
        setTranslucentStatus();
        initView();
        initLin();

    }
    private void initView() {
        tv_title = findViewById(R.id.tv_title);

        ContentDataBean.DataBean textContent = new Gson().fromJson(textData, new TypeToken<ContentDataBean.DataBean>() {}.getType());
        if (textContent != null){
            tv_title.setText(textContent.getAbout_us());
        }

        ll_reback = findViewById(R.id.ll_reback);
        webview = findViewById(R.id.webview);
        if ("en".equals(yuyan)){
            webview.loadUrl(domain+e_about);
        }else if ("ja".equals(yuyan)){
            webview.loadUrl(domain+j_about);
        }else{
            webview.loadUrl(domain+z_about);
        }


        WebSettings settings = webview.getSettings();
        settings.setDatabaseEnabled(true);
        settings.setLoadsImagesAutomatically(true); // 加载图片
        settings.setAllowFileAccess(true);
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setGeolocationEnabled(true);

        webview.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

    }

    private void initLin() {
        ll_reback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    /**
     * 设置状态栏背景状态
     */
    public  void setTranslucentStatus()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            Window win =  getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            win.setAttributes(winParams);
        }
        SystemStatusManager tintManager = new SystemStatusManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(0);//状态栏无背景
    }

}
